<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="数据日期" :required="true">
              <j-date placeholder="请选择数据日期" v-model="queryParam.sjrq"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="主题" :required="true">
              <j-tree-select
                ref="treeSelect"
                placeholder="请选择主题"
                v-model="queryParam.theme"
                dict="llbb_param_config,name,code"
                pidValue="1506488476828733442"
                hasChildField="has_child"
              >
              </j-tree-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleCreate" type="primary">报文生成</a-button>
      <a-button @click="handleUpload" type="primary">报文上报</a-button>
      <a-button @click="handleDown" type="primary">报文下载</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey='key'
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>
    <llbb-message-status-list-modal ref="messageStatusList"></llbb-message-status-list-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import Vue from 'vue'
import axios from 'axios'
import LlbbMessageStatusListModal from './LlbbMessageStatusListModel';
Vue.prototype.$axios = axios
import { getAction } from '@/api/manage'
import { ACCESS_TOKEN } from '@/store/mutation-types'

export default {
  name: 'messageCount',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    LlbbMessageStatusListModal
  },
  data() {
    return {
      description: '报表状态表管理页面',
      queryParam: {
        sjrq: '',
        theme: '0'
      },
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '数据日期',
          align: 'center',
          dataIndex: 'sjrq',
          customRender: function (text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '报文名称',
          align: 'center',
          dataIndex: 'reportcode_dictText'
        },
        {
          title: '主题分类',
          align: 'center',
          dataIndex: 'theme_dictText'
        },
        {
          title: '报文数量',
          align: 'center',
          dataIndex: 'count'
        },
        {
          title: '数据生成状态',
          align: 'center',
          dataIndex: 'createstate_dictText'
        },
        {
          title: '报文上报状态',
          align: 'center',
          dataIndex: 'reportstate_dictText'
        },
      ],
      url: {
        list: '/messagemanagement/llbbMessageManagement/selectMessageCount',
        delete: '/llbbMessageStatus/llbbMessageStatus/delete',
        deleteBatch: '/llbbMessageStatus/llbbMessageStatus/deleteBatch',
        exportXlsUrl: '/llbbMessageStatus/llbbMessageStatus/exportXls',
        importExcelUrl: 'llbbMessageStatus/llbbMessageStatus/importExcel'

      },
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    initDictConfig() {
    },
    handleCreate() {
      this.$refs.messageStatusList.show();
    },
    handleUpload() {
        const that = this;
        if(this.selectedRowKeys.length==0){
          that.$message.warning("请选择你要处理的数据");
          return false;
        }
        let checkPagam={
          sjrqs:[],
          reportcodes:[],
          theme:this.queryParam.theme
        }
        for(var i=0;i<this.selectionRows.length;i++){
          checkPagam.sjrqs[i]=this.selectionRows[i].sjrq;
          checkPagam.reportcodes[i]=this.selectionRows[i].reportcode;
        }
        var vurl =`${window._CONFIG['domianURL']}/llbbMessageStatus/llbbMessageStatus/sendFtp?theme=`+checkPagam.theme;
        vurl =vurl+"&sjrqs="+checkPagam.sjrqs+"&reportcodes="+checkPagam.reportcodes;
        this.loading = true;
        getAction(vurl,this.checkPagam).then((res)=>{
          that.onClearSelected();
          that.loading = false;
          if(res.success){
            that.$message.success(res.message);
            that.loadData(1);
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
        })
    },
    handleDown() {
        const that = this;
        if(this.selectedRowKeys.length==0){
          that.$message.warning("请选择你要处理的数据");
          return false;
        }
        let checkPagam={
          sjrqs:[],
          reportcodes:[],
          theme:this.queryParam.theme
        }
        for(var i=0;i<this.selectionRows.length;i++){
          checkPagam.sjrqs[i]=this.selectionRows[i].sjrq;
          checkPagam.reportcodes[i]=this.selectionRows[i].reportcode;
        }
        var vurl =`${window._CONFIG['domianURL']}/llbbMessageStatus/llbbMessageStatus/batchdown?theme=`+checkPagam.theme;
        vurl =vurl+"&sjrqs="+checkPagam.sjrqs+"&reportcodes="+checkPagam.reportcodes;
        this.$axios({
          method: "get",
          url: vurl,
          responseType: "blob",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'X-Access-Token':Vue.ls.get(ACCESS_TOKEN)
          },
        }).then(function (response) {
            //下载
            var reader = new FileReader();
            reader.onload = (e) => {
              //下载
              let data = response.data;
              if (!data) {
                return;
              }
              var blob = new Blob([data], {
                type: "text/plain;charset=UTF-8",
              });
              var url = window.URL.createObjectURL(blob);
              var aLink = document.createElement("a");
              aLink.style.display = "none";
              aLink.href = url;
              aLink.setAttribute("download", "报文数据.zip");
              document.body.appendChild(aLink);
              aLink.click();
              //下载完成移除元素
              document.body.removeChild(aLink);
              //释放掉blob对象
              window.URL.revokeObjectURL(url);
            };
            reader.readAsText(response.data);
          })
          .catch(function (error) {
            console.log(error);
          });
    },
    getSuperFieldList() {
      let fieldList = [];
      fieldList.push({ type: 'string', value: 'bankNoCd', text: '法人标识' })
      fieldList.push({ type: 'string', value: 'devideCls', text: '权限划分类别。按固定依据划分数据权限：1-角色2-机构3-用户' })
      fieldList.push({ type: 'string', value: 'bankCode', text: '分支机构编码' })
      this.superFieldList = fieldList
    }
  }
}
</script>

<style scoped>
</style>